/*********************************************************************************
Organization 					: 				Georgia Institute of Technology
  
Institute 						:				Cognitive Computing Group(CCL)
 
Authors							: 				Santiago Ontanon, Kinshuk Mishra
 												Neha Sugandh 
 												
Class							:				ResearchBOPlan
 
Function						: 				Provides a Basic Plan 
												for researching 
****************************************************************************/
package planninglanguage.plans;


import org.jdom.Element;

import planninglanguage.conditions.AndCondition;
import planninglanguage.conditions.Condition;
import planninglanguage.conditions.GoldCondition;
import planninglanguage.conditions.OilCondition;
import planninglanguage.conditions.OrCondition;
import planninglanguage.conditions.ResearchCondition;
import planninglanguage.conditions.StatusCondition;
import planninglanguage.conditions.UnitExistsCondition;
import planninglanguage.conditions.WoodCondition;
import util.XMLWriter;
import base.ProxyBot;
import base.WargusStateImporter;

public class ResearchBOPlan extends BasicOperatorPlan implements Cloneable{

	String m_research;
	
	public ResearchBOPlan(int a_unitID,String a_research,Condition a_precondition,Condition a_aliveCondition,Condition a_successCondition)
	{
		super(a_precondition,a_aliveCondition,a_successCondition);
		m_unitID=a_unitID;
		m_research=a_research;

		m_precondition = new AndCondition(new GoldCondition(WargusStateImporter.unitCostGold(m_research)),
         		  						  new AndCondition(new WoodCondition(WargusStateImporter.unitCostWood(m_research)),
         		  								  		   new OilCondition(WargusStateImporter.unitCostOil(m_research))));
		m_aliveCondition = new UnitExistsCondition(m_unitID);
		m_successCondition = new OrCondition(new ResearchCondition(m_research,0),
											 new StatusCondition(m_unitID,"research")); 
							
		
	}
	public ResearchBOPlan(int a_unitID,String a_research,Condition a_precondition,Condition a_aliveCondition,Condition a_successCondition, Plan a_parent)
	{
		super(a_precondition,a_aliveCondition,a_successCondition);
		m_unitID=a_unitID;
		m_research=a_research;

		m_precondition = new AndCondition(new GoldCondition(WargusStateImporter.unitCostGold(m_research)),
         		  						  new AndCondition(new WoodCondition(WargusStateImporter.unitCostWood(m_research)),
         		  								  		   new OilCondition(WargusStateImporter.unitCostOil(m_research))));
		m_aliveCondition = new UnitExistsCondition(m_unitID);
		m_successCondition = new OrCondition(new ResearchCondition(m_research,0),
											 new StatusCondition(m_unitID,"research")); 
		m_parent = a_parent;
							
		
	}
	
	public Object clone()
	{
		ResearchBOPlan l_p = new ResearchBOPlan(m_unitID,m_research,null,null,null, m_parent);
		l_p.setContext(m_context);
		l_p.m_originalUnit = m_originalUnit;
		return l_p;
	}
	
	public String getResearch(){
		return m_research;
	}
	
	public boolean send()
	{
		/*
		WargusInterface.m_action = WargusInterface.ACTION_RESEARCH;
		WargusInterface.m_unitid = m_unitID;
		WargusInterface.m_researchtype= GameStateImporter.researchStringToType(m_research);
		*/
		
		ProxyBot.getProxyBot().research(m_unitID, WargusStateImporter.researchStringToType(m_research));
		
		return true;
	}
	
	public String toString(int tabs) {
		String str= "";
		int i;
		
		for(i=0;i<tabs;i++) str = str + "  ";
		str = str + "ResearchBOPlan(status = " + STATUS_NAME[m_status] + " , unitID = " + m_unitID +  " , research = " + m_research + " ) ";
		str = str + conditionsToString(tabs);
		return str;
	}	
	
	public boolean sameGoal(Object other)
	{
		if (getClass() != other.getClass()) return false;

		ResearchBOPlan o = (ResearchBOPlan) other;
		   
		if (m_unitID!=o.m_unitID) return false;	
		if (!m_research.equals(m_research)) return false;
		
		return true;
	}		
	
	public void writeXML(XMLWriter w) {
		w.tag(super.XMLopenTag() + " type=\"" + 
				this.getClass().getSimpleName() + "\"");		
		{
			w.tag(Plan.XML_VALUE, m_research);
			
			super.writeBOPlanAttrsXML(w);
		}
		w.tag(super.XMLcloseTag());
	}
	
	protected static ResearchBOPlan loadFromXML(Element e) {
		String research = e.getChildText(Plan.XML_VALUE);
		
		ResearchBOPlan plan = new ResearchBOPlan(-1, research, null, null, null);
		return plan;
	}
}
